/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-11-18 20:50:12
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../utils/timeFormart");
class KnowledgeService extends Service {
    //获取文章数据 +分页 +搜索
    async getKnowledgeList() {
            const { ctx, app } = this;
            const { pageSize, pageIndex, title, state } = ctx.request.body;
            let res = await app.mysql.select("knowledge");
            let total = res.length;
            //名称
            if (title) {
                res = res.filter((item) => {
                    if (item.title.indexOf(title) !== -1 || item.title.includes(title)) {
                        return item;
                    }
                });
                total = res.length;
            }
            //状态
            if (state !== null) {
                res = res.filter((item) => {
                    return item.state * 1 == state * 1;
                });
                total = res.length;
            }
            //分页
            res.sort((a, b) => {
                return new Date(b.create_time) - new Date(a.create_time);
            });
            res = res.slice((pageIndex - 1) * pageSize, pageSize * pageIndex);
            return { res, total };
        }
        //添加
    async addKnowledgeList() {
            const { ctx, app } = this;
            const { title, desc, state, is_allow_comment, cover } = ctx.request.body;
            let res = await app.mysql.insert("knowledge", {
                ...ctx.request.body,
                create_time: timeFormart(Date.now()),
            });
            return res;
        }
        //删除
    async delKnowledgeList() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.delete("knowledge", { id });
            return res;
        }
        //删除
    async editKnowledgeList() {
            const { ctx, app } = this;
            const { id, title, desc, state, is_allow_comment, cover, children } =
            ctx.request.body;
            const res = await app.mysql.update("knowledge", {
                id,
                ...ctx.request.body,
            });
            return res;
        }
        //改变线上状态
    async changeKnowledgeState() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            //找到原数据
            const res = await app.mysql.get("knowledge", {
                id,
            });
            //判断状态
            if (res.state * 1 === 1) {
                //更新文章状态
                await app.mysql.update("knowledge", { id, state: 0 });
            } else {
                //更新文章状态
                await app.mysql.update("knowledge", { id, state: 1 });
            }
            return "操作成功";
        }
        //获取详情（一级）
    async getKnowledgeDetail() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.get("knowledge", { id });
            return res;
        }
        //获取二级内容
    async getKnowledgeChildren() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            let res = await app.mysql.select("knowledge_children");
            res = res.filter((item) => item.knowledge_id * 1 === id * 1);
            res.sort((a, b) => {
                return new Date(b.create_time) - new Date(a.create_time);
            });
            let total = res.length;
            return {
                data: res,
                total,
            };
        }
        //     //添加二级内容
    async addKnowledgeChildren() {
        const { ctx, app } = this;
        const { addList } = ctx.request.body;
        addList.forEach((item) => {
            delete item.id;
        });
        addList.forEach((item) => {
            app.mysql.insert("knowledge_children", {
                ...item,
                create_time: timeFormart(Date.now()),
            });
        });
        return "操作成功";
    }
}
module.exports = KnowledgeService;